package com.dgut.legalservice.controller;

import com.dgut.legalservice.domain.Order;
import com.dgut.legalservice.domain.OrderVo;
import com.dgut.legalservice.domain.User;
import com.dgut.legalservice.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 显示用户所有的订单 模糊查询
     * @return
     */
    @RequestMapping("/all")
    public ModelAndView findOrders(HttpSession session, OrderVo orderVo){
        User user =(User) session.getAttribute("user");
        int userId = user.getId();
        orderVo.setUserId(userId);
        orderVo.setType("普通用户");
        ModelAndView mv = new ModelAndView();
        List<Order> orders = orderService.selectOrders(orderVo);

        /**
         * 迭代器修改集合对象
         */
        Iterator<Order> it = orders.iterator();
        while (it.hasNext()){
            Order order = it.next();
            if (order.getProducts().size()==0){
                it.remove();
            }
        }

        System.out.println("显示用户订单！");
        for (Order order : orders) {
            System.out.println(order);
        }
        mv.addObject("orders",orders);
        mv.setViewName("/user/order-list");

        return mv;
    }

    /**
     * 显示服务商订单 模糊查询
     * @param orderVo
     * @return
     */
    @RequestMapping("/allser")
    public ModelAndView findSers(OrderVo orderVo) {
        orderVo.setType("服务商");
        orderVo.setStatus("平台方已审核");
        if (orderVo.getOrderNum() != null) {
            String orderNum = orderVo.getOrderNum();
            orderVo.setOrderNum("%"+orderNum+"%");
        }
        ModelAndView mv = new ModelAndView();
        List<Order> orders = orderService.selectOrders(orderVo);

        /**
         * 迭代器修改集合对象
         */
        Iterator<Order> it = orders.iterator();
        while (it.hasNext()){
            Order order = it.next();
            if (order.getProducts().size()==0){
                it.remove();
            }
        }

        System.out.println("显示服务商订单");
        for (Order order : orders) {
            System.out.println(order);
        }
        mv.addObject("orders",orders);
        mv.setViewName("/provider/order-list.html");

        return mv;
    }


    /**
     * 显示平台方订单 模糊查询
     * @param orderVo
     * @return
     */
    @RequestMapping("/allpla")
    public ModelAndView findPlas(OrderVo orderVo){
        orderVo.setType("平台方");
        orderVo.setStatus("平台方未审核");
        ModelAndView mv = new ModelAndView();
        List<Order> orders = orderService.selectOrders(orderVo);

        /**
         * 迭代器修改集合对象
         */
        Iterator<Order> it = orders.iterator();
        while (it.hasNext()){
            Order order = it.next();
            if (order.getProducts().size()==0){
                it.remove();
            }
        }

        System.out.println("显示平台方订单！");
        for (Order order : orders) {
            System.out.println(order);
        }
        mv.addObject("orders",orders);
        mv.setViewName("/platform/order-list.html");

        return mv;
    }
    /**
     * 用户更新支付状态
     * @param id
     * @param payStatus
     * @param session
     * @param orderVo
     * @return
     */
    @RequestMapping("/update/{payStatus}/{id}")
    public ModelAndView payFir(@PathVariable String id,@PathVariable String payStatus,HttpSession session,OrderVo orderVo){
        User user =(User) session.getAttribute("user");
        int userId = user.getId();
        orderVo.setUserId(userId);
        ModelAndView mv = new ModelAndView();
        int y =Integer.parseInt(id);
        int i = orderService.payFir(y,payStatus);
        List<Order> orders = orderService.selectOrders(orderVo);

        /**
         * 迭代器修改集合对象
         */
        Iterator<Order> it = orders.iterator();
        while (it.hasNext()){
            Order order = it.next();
            if (order.getProducts().size()==0){
                it.remove();
            }
        }
        mv.addObject("orders",orders);
        mv.setViewName("/user/order-list.html");

        return mv;
    }

    /**
     * 设置订单流程 抢单流程
     * @param process
     * @param orderId
     * @param session
     * @return
     */
    @RequestMapping("/set/{process}/{orderId}")
    public ModelAndView setPr(@PathVariable String process, @PathVariable String orderId,HttpSession session) {
        User user = (User)session.getAttribute("user");
        Order order = new Order();
        order.setStatus(process);

        if ("抢单成功".equals(process)){                //抢单状态需要改变接单人
            System.out.println("抢单执行了！");
            order.setAcceptUserId(user.getId());
        }

        int i = Integer.parseInt(orderId);
        int firStatus = orderService.setFirStatus(order, i);

        ModelAndView mv = new ModelAndView();

        if ("普通用户".equals(user.getType())){
            OrderVo orderVo = new OrderVo();

            int userId = user.getId();
            orderVo.setUserId(userId);

            List<Order> orders = orderService.selectOrders(orderVo);
            System.out.println("输出用户的订单");
            for (Order order1 : orders) {
                System.out.println(order1);
            }
            mv.addObject("orders",orders);
            mv.setViewName("/user/order-list.html");

            return mv;
        }else if ("服务商".equals(user.getType())){
            OrderVo orderVo = new OrderVo();
            if ("抢单成功".equals(process)){
                System.out.println("进入抢单跳转页面");
                orderVo.setUserId(user.getId());
                orderVo.setStatus(process);
                orderVo.setType(user.getType());
                List<Order> orders = orderService.selectOrders(orderVo);
                /**
                 * 迭代器修改集合对象
                 */
                Iterator<Order> it = orders.iterator();
                while (it.hasNext()){
                    Order order1 = it.next();
                    if (order1.getProducts().size()==0||order1.getCompanies().size()==0){
                        it.remove();
                    }
                }
                mv.addObject("orders",orders);
                mv.setViewName("/provider/order_sucess.html");
                return mv;
            }else {
                orderVo.setStatus("平台方已审核");

                List<Order> orders = orderService.selectOrders(orderVo);
                for (Order order1 : orders) {
                    System.out.println(order1);
                }
                mv.addObject("orders", orders);
                mv.setViewName("/provider/order-list.html");

                return mv;
            }
        } else if ("超级管理员".equals(user.getType())) {
            OrderVo orderVo = new OrderVo();
            orderVo.setStatus("平台方未审核");
            System.out.println("平台方请求转发！");
            List<Order> orders = orderService.selectOrders(orderVo);
            for (Order order1 : orders) {
                System.out.println(order1);
            }
            mv.addObject("orders",orders);
            mv.setViewName("/platform/order-list.html");

            return mv;
        }else{

        }
        mv.setViewName("/order/header.html");
        return mv;
    }

    /**
     * 删除订单
     * @param id
     * @param session
     * @return
     */
    @RequestMapping("/delete/{id}")
    public ModelAndView deleteOrder(@PathVariable String id,HttpSession session) {
        int i = Integer.parseInt(id);
        orderService.deleteOrder(i);

        User user =(User) session.getAttribute("user");
        int userId = user.getId();
        OrderVo orderVo = new OrderVo();
        orderVo.setUserId(userId);
        List<Order> orders = orderService.selectOrders(orderVo);
        ModelAndView mv = new ModelAndView();
        mv.addObject("orders",orders);
        if ("普通用户".equals(user.getType())){
            mv.setViewName("/user/order-list.html");
            return mv;
        }else {
            mv.setViewName("/platform/order-list.html");
            return mv;
        }

    }

    /**
     * 查询某种状态订单
     * @return
     */
    @RequestMapping("/find/{process}")
    public ModelAndView findStat(@PathVariable String process,HttpSession session){
        User user =(User) session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        OrderVo orderVo = new OrderVo();
        orderVo.setType(user.getType());
        orderVo.setStatus(process);
        orderVo.setUserId(user.getId());
        List<Order> orders = orderService.selectOrders(orderVo);

        /**
         * 迭代器修改集合对象
         */
        Iterator<Order> it = orders.iterator();
        while (it.hasNext()){
            Order order = it.next();
            if (order.getProducts().size()==0){
                it.remove();
            }
        }

        if ("普通用户".equals(user.getType())){


            mv.addObject("orders",orders);
            mv.setViewName("/user/order-list.html");

            return mv;
        }else if ("服务商".equals(user.getType())){
            if("抢单成功".equals(process)){


                mv.addObject("orders",orders);
                mv.setViewName("/provider/order_sucess.html");
                return mv;
            }else {
                mv.addObject("orders", orders);
                mv.setViewName("/provider/order-list.html");
                return mv;
            }

        } else if ("超级管理员".equals(user.getType())) {
            orderVo.setStatus("平台方未审核");

            mv.addObject("orders",orders);
            mv.setViewName("/platform/order-list.html");

            return mv;
        }else{
            mv.setViewName("");
            return mv;
        }
    }


}
